# -*- coding: utf-8 -*-
from datetime import datetime

from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, UniqueConstraint

from db.base import Base
from db.models.base import BaseDatabaseModel, GUID


class FileAttributeAssociation(BaseDatabaseModel, Base):
    __tablename__ = "file_attribute_associations"

    id = Column(Integer, primary_key=True, index=True)
    file_id = Column(GUID(), ForeignKey('qa_files.id'), index=True, comment="文件ID")
    attribute_id = Column(Integer, index=True, comment="属性ID")
    attribute_type = Column(String(50), index=True, comment="属性类型: applicable_object/knowledge_type/applicable_area/element_classification")

    create_time = Column(DateTime, default=datetime.now)  # 创建时间

    # 添加唯一约束，防止重复关联
    __table_args__ = (
        UniqueConstraint('file_id', 'attribute_id', 'attribute_type', name='uq_file_attribute'),
    )


class ApplicableObject(BaseDatabaseModel, Base):
    __tablename__ = "applicable_object"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=True, comment="适用对象名称： 全部-0/油气化工码头-1/...")

    def to_json(self):
        return {
            "id": self.id,
            "name": self.name
        }


class KnowledgeType(BaseDatabaseModel, Base):
    __tablename__ = "knowledge_type"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=True, comment="知识类型： 法律-1/法规-2/标准-3/文件-4/书籍-5/专家知识-6/...")

    def to_json(self):
        return {
            "id": self.id,
            "name": self.name
        }


class ApplicableArea(BaseDatabaseModel, Base):
    __tablename__ = "applicable_area"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=True, comment="适用区域：全国-0/河北-1/...")

    def to_json(self):
        return {
            "id": self.id,
            "name": self.name
        }


class ElementClassification(BaseDatabaseModel, Base):
    __tablename__ = "element_classification"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=True, comment="要素分类：设计-1/供电照明-2/...")

    def to_json(self):
        return {
            "id": self.id,
            "name": self.name
        }


class AccidentType(BaseDatabaseModel, Base):
    __tablename__ = "accident_type"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(32), nullable=False, comment="事故类型名称")

    def to_json(self):
        return {
            "id": self.id,
            "name": self.name
        }
